﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms
{
    public partial class frmExtTexts : Form
    {
        private int disciplineListId = 0, performanceId = 0;
        private string sTitle = string.Empty;

        // Constructor
        public frmExtTexts(int disciplineListId, int performanceId)
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;

            if (disciplineListId != 0)
            {
                this.disciplineListId = disciplineListId;
                this.sTitle = DatabaseHelper.GetDisciplineListById(disciplineListId).Rows[0]["Name"].ToString();

                this.performanceId = 0;
            }

            if (performanceId != 0)
            {
                this.disciplineListId = 0;
                
                this.performanceId = performanceId;
                DataTable dtPerformance = DatabaseHelper.GetPerformancesById(performanceId);
                this.sTitle = dtPerformance.Rows[0]["Surname"].ToString() + " " + dtPerformance.Rows[0]["FirstName"].ToString();
            }
        }

        // Form Load
        private void frmExtTexts_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = this.sTitle;

            if (this.disciplineListId != 0)
            {
                DatabaseHelper.FillExtTextsCodesForDisciplineList(this.aDbDataSet.ExtTextsCodes);
                DatabaseHelper.FillExtTextsByDisciplineListId(this.aDbDataSet.ExtTexts, this.disciplineListId);
            }

            if (this.performanceId != 0)
            {
                DatabaseHelper.FillExtTextsCodesForPerformance(this.aDbDataSet.ExtTextsCodes);
                DatabaseHelper.FillExtTextsByPerformanceId(this.aDbDataSet.ExtTexts, this.performanceId);
            }
        }

        // Ulozit
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.bsExtTexts.EndEdit();

                if (this.disciplineListId != 0)
                {
                    foreach (DataRow dr in this.aDbDataSet.ExtTexts.Rows)
                    {
                        if (dr.RowState == DataRowState.Added)
                        {
                            dr["DisciplineListId"] = this.disciplineListId;
                        }

                        this.SetRowData(dr);
                    }
                }

                if (this.performanceId != 0)
                {
                    foreach (DataRow dr in this.aDbDataSet.ExtTexts.Rows)
                    {
                        if (dr.RowState == DataRowState.Added)
                        {
                            dr["PerformanceId"] = this.performanceId;
                        }
                        
                        this.SetRowData(dr);
                    }
                }

                this.taExtTexts.Update(this.aDbDataSet.ExtTexts);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Chyba pri ukladaní: " + ex.Message, "Doplňujúce texty", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Koniec
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetRowData(DataRow dr)
        {
            if (dr.RowState == DataRowState.Added)
            {
                dr["Modified"] = DateTime.Now;
                dr["Active"] = 1;

                if (dr["Sync"] == DBNull.Value)
                    dr["Sync"] = false;

                if (dr["WebId"] == DBNull.Value)
                    dr["WebId"] = 0;

                if (dr["Code"] == DBNull.Value)
                    dr["Code"] = 1;
            }
            else if (dr.RowState == DataRowState.Modified)
            {
                dr["Modified"] = DateTime.Now;

                if (dr["Code"] == DBNull.Value)
                    dr["Code"] = 1;
            }
            else if (dr.RowState == DataRowState.Deleted)
            {
                dr.RejectChanges();
                dr.SetModified();
                dr["Active"] = 0;
                dr["Modified"] = DateTime.Now;
            }
        }



    }
}
